package com.zhonglian.elisa.customer;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.zhonglian.elisa.R;
import com.zhonglian.elisa.utils.LogUtil;
import com.zhonglian.elisa.utils.OpenCvUtils;
import com.zhonglian.elisa.view.d.bean.ReagentStripData;

import java.util.ArrayList;
import java.util.List;


/**
 * 8个试剂条展示
 */
public class ReagentStripView extends LinearLayout {
    private OnVieweListener listener;

    public void setOnVieweListener(OnVieweListener listener) {
        this.listener = listener;
    }

    public ReagentStripView(Context context) {
        super(context);
    }

    public ReagentStripView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        //setPadding(0, dpToPx(15), dpToPx(20), dpToPx(15));

        for (int i = 0; i < 8; i++) {
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT);
            params.weight = 1;
            imageView.setLayoutParams(params);  //设置图片宽高

            imageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.color_f3_drawable)); //图片资源
            addView(imageView);//动态添加图片
        }
    }

    //多条展示
    public void setImgData(Bitmap bitmap, List<ReagentStripData> imgData) {
        if (imgData == null || imgData.size() == 0) {
            for (int i = 0; i < 8; i++) {
                ImageView imgView = (ImageView) this.getChildAt(i);
                imgView.setImageDrawable(null);
            }
            return;
        }
        for (int i = 0; i < imgData.size(); i++) {
            ImageView imgView = (ImageView) this.getChildAt(i);
            Bitmap bitmap2 = Bitmap.createBitmap(bitmap, imgData.get(i).iStartX, imgData.get(i).iStartY, imgData.get(i).iWidth, imgData.get(i).iHeight);
            imgView.setImageBitmap(bitmap2);
        }
    }

    //单条展示，默认第4条
    public void setImgData(Bitmap bitmap, ReagentStripData imgData) {
        for (int i = 0; i < 8; i++) {
            ImageView imgView = (ImageView) this.getChildAt(i);
            imgView.setImageDrawable(null);
        }
        ImageView imgView = (ImageView) this.getChildAt(3);
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, imgData.iStartX, imgData.iStartY, imgData.iWidth, imgData.iHeight);
        imgView.setImageBitmap(bitmap2);
    }

    /**
     * imageview获取drawable并转换为 bitmap ：Bitmap bt= ((BitmapDrawable) mImageview.getDrawable()).getBitmap();
     * resourceid转换为bitmap：Bitmap bt = BitmapFactory.decodeResource(getResources(), R.drawable.resourceid);
     * Drawable转换为bitmap：Bitmap bt= ((BitmapDrawable) Drawable).getBitmap();
     * 因为BitmapDrawable是继承Drawable，所以可以灵活的转换
     * <p>
     * position 将要处理的图片下标 默认第4条下标3
     */
    public Bitmap getImgBitmap(int position) {
        ImageView imageView = (ImageView) this.getChildAt(position);
        return ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    }

    public void setImgBitmap(int position, Bitmap bitmap) {
        ImageView imageView = (ImageView) this.getChildAt(position);
        imageView.setImageBitmap(bitmap);
    }

    /**
     * 获取Bitmap在ImageView中的偏移量数组,其中第0个值表示在水平方向上的偏移值,第1个值表示在垂直方向上的偏移值
     *
     * @param imageView
     * @param includeLayout 在计算偏移的时候是否要考虑到布局的因素,如果要考虑该因素则为true,否则为false
     * @return the offsets of the bitmap inside the imageview, offset[0] means horizontal offset, offset[1] means vertical offset
     * https://www.jianshu.com/p/df6deea80c2c
     */
    public int[] getBitmapOffset(ImageView imageView, boolean includeLayout) {
        if (imageView == null) imageView = (ImageView) this.getChildAt(3);
        int[] offset = new int[2];
        float[] values = new float[9];

        Matrix matrix = imageView.getImageMatrix();
        matrix.getValues(values);

        // x方向上的偏移量(单位px)
        offset[0] = (int) values[2];
        // y方向上的偏移量(单位px)
        offset[1] = (int) values[5];

        if (includeLayout) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageView.getLayoutParams();
            int paddingTop = imageView.getPaddingTop();
            int paddingLeft = imageView.getPaddingLeft();

            offset[0] += paddingLeft + params.leftMargin;
            offset[1] += paddingTop + params.topMargin;
        }
        return offset;
    }


    public interface OnVieweListener {
        void onCallback(int position);

    }


    public int dpToPx(int dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

}
